package cn.liangent.travlib;

import static cn.liangent.travlib.util.Utilities.*;
import java.net.*;
import java.nio.charset.*;
import java.util.*;

/**
 * TravianUser is immutable.
 * @author liangent
 */
// do we need to limit one user can tie to one client only?
public class User {
	
	private final URL server;
	private final ResourceBundle bundle;
	private final Charset charset;
	private final String username, password;
	
	public User(URL server, ResourceBundle bundle, String username, String password) {
		this(server, bundle, username, password, null);
	}
	
	/**
	 * @param server like <code>new URL("http://s1.travian.com/")</code>
	 * @param charset only this can be null
	 */
	public User(URL server, ResourceBundle bundle, String username, String password, Charset charset) {
		if (server == null ||
			bundle == null ||
			username == null ||
			password == null
		) throw new NullPointerException();
		checkResourceBundle(bundle);
		this.server = server;
		this.bundle = bundle;
		try {
			this.charset = (charset == null ? Charset.forName("UTF-8") : charset);
		} catch (Exception ex) {
			throw new UnsupportedOperationException("JVM should support UTF-8.", ex); // a more accurate Error?
		}
		this.username = username;
		this.password = password;
	}
	
	public URL getServer() {
		return server;
	}
	
	public String getString(String key) {
		return bundle.getString(key.trim().toLowerCase().replaceAll("[\\p{javaWhitespace}\\p{Punct}\\p{Cntrl}]", "_"));
	}
	
	public Charset getCharset() {
		return charset;
	}
	
	public String getUsername() {
		return username;
	}
	
	public String getPassword() {
		return password;
	}

	// equals(Object) is removed because ResourceBundle doesn't support correct equals(Object)
}
